package com.xuhuan.juc.thread;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author huan.xu
 * @Date 2021/12/28 16:59
 * @Description 线程通信
 * 题目：现在两个线程，可以操作初始值为零的一个变量，
 * 实现一个线程对该变量加1，一个线程对该变量减1，
 * 实现交替，来10轮，变量初始化值为0
 * <p>
 * 判断/干活/通知
 * 多线程交互中，必须要防止多线程的虚假唤醒，也即判断只能用while，不能用if
 */
public class ThreadWaitNotifyDemo {

    public static void main(String[] args) {
        AirConditioner airConditioner = new AirConditioner();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airConditioner.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程1").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airConditioner.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程2").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airConditioner.decrment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程3").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airConditioner.decrment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程4").start();
    }
}

@Slf4j
@Data
/**
 * 空调
 */
class AirConditioner {
    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    /**
     * 加一度
     *
     * @throws InterruptedException
     */
    public void increment() throws InterruptedException {
        lock.lock();
        try {
            //1.判断是否执行
            while (number != 0) {
                condition.await();
            }
            //2.干活
            number++;
            log.info("{}\t{}", Thread.currentThread().getName(), number);
            //3.通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    /**
     * 减一度
     *
     * @throws InterruptedException
     */
    public void decrment() throws InterruptedException {
        lock.lock();
        try {
            //1.判断是否执行
            while (number == 0) {
                condition.await();
            }
            //2.干活
            number--;
            log.info("{}\t{}", Thread.currentThread().getName(), number);
            //3.通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
